class Solution:
    def constructDistancedSequence(self, n: int) -> list:
        def func(array, i, n, memo):
            if i > len(array) - 1:
                return True
            if array[i] > 0:
                return func(array, i + 1, n, memo)
            for j in range(n, 0, -1):
                if j in memo:
                    continue
                if j > 1:
                    if i + j >= len(array) or array[i + j] > 0:
                        continue
                array[i] = j
                if j > 1:
                    array[i + j] = j
                memo.add(j)
                if func(array, i + 1, n, memo):
                    return True
                array[i] = 0
                if j > 1:
                    array[i + j] = 0
                memo.remove(j)

            return False

        array = [0 for _ in range(2 * n - 1)]
        memo = set()
        func(array, 0, n, memo)
        return array
